package cn.wildfire.chat.kit.user;

import android.content.Intent;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;

import java.util.HashMap;

import butterknife.BindView;
import cn.wildfire.chat.app.extendsion.adapters.CommonGroupAdapter;
import cn.wildfire.chat.app.extendsion.classes.RequestCommonGroupEntity;
import cn.wildfire.chat.app.extendsion.classes.ResponseEntity;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

/**
 * @author Lizhong
 * TODO 共同群
 */
public class OurCommonGroupActivity extends WfcBaseActivity {

    @BindView(R2.id.rv_common_group)
    RecyclerView rvCommonGroup;

    CommonGroupAdapter adapter;
    UserInfo friendUserInfo;
    @BindView(R2.id.tv_group_count)
    TextView tvGroupCount;

    @Override
    protected void afterViews() {
        super.afterViews();
        Intent intent = getIntent();
        friendUserInfo = intent.getParcelableExtra("userInfo");

        rvCommonGroup.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CommonGroupAdapter(this, R.layout.item_common_group);
        rvCommonGroup.setAdapter(adapter);


        getDate();
    }

    private void getDate() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("frienduid", friendUserInfo.uid);
        params.put("uid", ChatManager.Instance().getUserId());
        OkHttpRequest.getInstance(this).requestAsynJsonObject(ServerUrlClient.CHATTING_TOGETHER, params, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                ResponseEntity<JsonObject, String> responseEntity = new ResponseEntity<>(result.toString());
                if (responseEntity.getErrorCode().equals("0")) {
                    Gson gson = new Gson();
                    RequestCommonGroupEntity requestCommonGroupEntity = gson.fromJson(result.toString(), RequestCommonGroupEntity.class);
                    adapter.setNewData(requestCommonGroupEntity.getResult());
                    tvGroupCount.setText(String.valueOf(requestCommonGroupEntity.getResult().size()) + getString(R.string.ge_group_chat));
                } else {

                }
            }

            @Override
            public void onReqFailed(String errorMsg) {

            }
        }).request();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_our_common_group;
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 3);
        setReplaceTitle(getTitle().toString(), -1);
        setSearchLayoutVisibility(false);
    }
}

